layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
        elem: '#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem: '#endTime'
        ,value:new Date()
    });

    /**
     * 承运商运费对账单管理
     */
    var FiCarrierFreightInfo = {
        tableId: "fiCarrierFreightInfoTable"
    };

    /**
     * 初始化表格的列
     */
    FiCarrierFreightInfo.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {align: 'center', toolbar: '#tableBar', title: '操作',width:180},
            {field: 'operateName', sort: true, title: '所属项目',width:140},
            {field: 'startDt', sort: true, title: '开始日期',width:130},
            {field: 'endDt', sort: true, title: '截至日期',width:130},
            {field: 'carrierName', sort: true, title: '承运商',width:100},
            {field: 'lineName', sort: true, title: '线路名称',width:160, totalRowText: '合计：'},
            {field: 'kilometer', sort: true, title: '公里数',width:160, totalRow: true},
            {field: 'sumNum', sort: true, title: '总数量',width:160, totalRow: true},
            {field: 'sumVol', sort: true, title: '总体积',width:160, totalRow: true},
            {field: 'freightMoney', sort: true, title: '运费',width:160, totalRow: true},
            {field: 'custComplaint', sort: true, title: '客诉',width:160, totalRow: true},
            {field: 'otherFines', sort: true, title: '其他罚款',width:160, totalRow: true},
            {field: 'otherSubsidie', sort: true, title: '其他补贴',width:160, totalRow: true},
            {field: 'loadingMoney', sort: true, title: '装车费',width:160, totalRow: true},
            {field: 'sumMoney', sort: true, title: '总费用',width:160, totalRow: true},
            {field: 'stat', sort: true, title: '状态',width:160},
            {field: 'createTime', sort: true, title: '创建时间',width:160}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FiCarrierFreightInfo.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['operateName'] = $("#operateName").val();
        queryData['carrierName'] = $("#carrierName").val();
        queryData['lineName'] = $("#lineName").val();
        queryData['stat'] = $("#stat").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();

        table.reload(FiCarrierFreightInfo.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // // 搜索按钮点击事件
                 // $('#btnSearch').click(function () {
                 //     FiCarrierFreightInfo.search();
                 // });
                 // // 添加按钮点击事件
                 // $('#btnAdd').click(function () {
                 //     FiCarrierFreightInfo.openAddDlg();
                 // });
                 // // 导出excel
                 // $('#btnExp').click(function () {
                 //     FiCarrierFreightInfo.exportExcel();
                 // });
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    FiCarrierFreightInfo.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/fiCarrierFreightInfo/add';
    };

    /**
     * 弹出添加对话框
     */
    FiCarrierFreightInfo.openAddByCarrierDlg = function () {
        window.location.href = Feng.ctxPath + '/fiCarrierFreightInfo/addByCarrier';
    };


    /**
     * 导出excel按钮
     */
    FiCarrierFreightInfo.exportExcel = function () {
        var checkRows = table.checkStatus(FiCarrierFreightInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    FiCarrierFreightInfo.batchDuizhang = function () {
        var checkRows = table.checkStatus(FiCarrierFreightInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要对账的数据");
        } else {
            var ids = checkRows.data.map(function(item) {
                return item.id;
            });

            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/fiCarrierFreightInfo/duizhangBatch", function (data) {
                    if(data.success){
                        Feng.success("操作成功!");
                        table.reload(FiCarrierFreightInfo.tableId);
                    }else{
                        Feng.error("操作失败!" + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("操作失败!" + data.responseJSON.message + "!");
                });
                ajax.set("ids", ids.join(","));
                ajax.start();
            };
            Feng.confirm("是否对选择的记录发起对账?", operation);
        }
    };


    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    FiCarrierFreightInfo.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/fiCarrierFreightInfo/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FiCarrierFreightInfo.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/fiCarrierFreightInfo/delete", function (data) {
                if(data.success){
                    Feng.success("删除成功!");
                    table.reload(FiCarrierFreightInfo.tableId);
                }else{
                    Feng.error("删除失败!" + data.message + "!");
                }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    FiCarrierFreightInfo.onDuizhangItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/fiCarrierFreightInfo/duizhang", function (data) {
                if(data.success){
                    Feng.success("操作成功!");
                    table.reload(FiCarrierFreightInfo.tableId);
                }else{
                    Feng.error("操作失败!" + data.message + "!");
                }
            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.set("stat", "对账中");
            ajax.start();
        };
        Feng.confirm("是否发起对账?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FiCarrierFreightInfo.tableId,
        url: Feng.ctxPath + '/fiCarrierFreightInfo/list',
        page: true,
        limit: 20,
        height: "full-100",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: FiCarrierFreightInfo.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FiCarrierFreightInfo.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        FiCarrierFreightInfo.openAddDlg();
    });
    // 添加按钮点击事件
    $('#btnAddByCarrier').click(function () {
        FiCarrierFreightInfo.openAddByCarrierDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        FiCarrierFreightInfo.exportExcel();
    });

    // 批量对账
    $('#btnduizhang').click(function () {
        FiCarrierFreightInfo.batchDuizhang();
    });


    // 工具条点击事件
    table.on('tool(' + FiCarrierFreightInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FiCarrierFreightInfo.openEditDlg(data);
        } else if (layEvent === 'delete') {
            FiCarrierFreightInfo.onDeleteItem(data);
        } else if (layEvent === 'duizhang') {
            FiCarrierFreightInfo.onDuizhangItem(data);
        }
    });
});
